﻿using System;
using System.Web;
using System.Web.UI;
using eShop.Business;
using eShop.Model;
using eShop.Utility;

public partial class admin_controls_manufacturer_form : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindFormDataToUI();
            BindModelToUI();
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {
                SaveManufacturer();
                uccResponseMessage.SetSuccessMessage("Data saved successfully");
                uccResponseMessage.Visible = true;
            }
        }
        catch (Exception exception)
        {
            uccResponseMessage.SetFailMessage("Failed to save data." + "<br />" + exception.Message);
            uccResponseMessage.Visible = true;
        }
        finally
        {
            BindModelToUI();
        }
    }

    private void BindFormDataToUI()
    {
    }

    private void BindModelToUI()
    {
        if (Id > 0)
        {
            Manufacturer manufacturer = LoadManufacturer();
            BindManufacturerToUI(manufacturer);
        }
        else
        {
            ClearUI();
        }
    }

    private void ClearUI()
    {
        tbxBrandName.Text = string.Empty;
        tbxDescription.Text = string.Empty;
        cbxIsActive.Checked = false;
        imgDefaultImagePreview.ImageUrl = string.Empty;
        imgDefaultImagePreview.Visible = false;
        imgDefaultBannerPreview.ImageUrl = string.Empty;
        imgDefaultBannerPreview.Visible = false;
    }

    private void BindManufacturerToUI(Manufacturer manufacturer)
    {
        tbxBrandName.Text = manufacturer.BrandName;
        tbxDescription.Text = manufacturer.BrandDescription;
        cbxIsActive.Checked = manufacturer.IsActive;
        if (!string.IsNullOrEmpty(manufacturer.BrandImageUrl))
        {
            imgDefaultImagePreview.ImageUrl = manufacturer.BrandImageUrl;
            imgDefaultImagePreview.Visible = true;
        }
        if (!string.IsNullOrEmpty(manufacturer.BrandBannerImageUrl))
        {
            imgDefaultBannerPreview.ImageUrl = manufacturer.BrandBannerImageUrl;
            imgDefaultBannerPreview.Visible = true;
        }
    }

    private Manufacturer LoadManufacturer()
    {
        if (Id > 0)
        {
            return ManufacturerManager.GetManufacturerById(Id);
        }
        else
        {
            return new Manufacturer();
        }
    }

    private void SaveManufacturer()
    {
        Manufacturer manufacturer = LoadManufacturer();
        manufacturer.BrandName = tbxBrandName.Text.Trim();
        manufacturer.BrandDescription = tbxDescription.Text.Trim();
        manufacturer.IsActive = cbxIsActive.Checked;
        if (fluPartnerImage.HasFile)
        {
            manufacturer.BrandImageUrl = WebPresentationUtility.UploadImageAndValidate(ApplicationSettings.TemporaryFolderPath, fluPartnerImage.PostedFile);
        }
        if (fluPartnerBannerImage.HasFile)
        {
            manufacturer.BrandBannerImageUrl = WebPresentationUtility.UploadImageAndValidate(ApplicationSettings.TemporaryFolderPath, fluPartnerBannerImage.PostedFile);
        }

        if (manufacturer.ProductBrandId > 0)
        {
            ManufacturerManager.UpdateManufacturer(manufacturer);
        }
        else
        {
            ManufacturerManager.CreateManufacturer(manufacturer);
        }
    }

    private Int32? _id;
    private Int32 Id
    {
        get
        {
            if (!_id.HasValue)
            {
                try
                {
                    if (HttpContext.Current.Request.QueryString != null && !String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["id"]))
                    {
                        _id = Convert.ToInt32(HttpContext.Current.Request.QueryString["id"]);
                    }
                    else
                    {
                        _id = -1;
                    }
                }
                catch
                {
                    _id = -1;
                }
            }
            return _id.Value;
        }
    }
}